
/*
 * CopyPanel.java
 *
 * Created on December 2, 2007, 3:04 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package filecopier.gui.tabs;

import java.io.File;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import filecopier.Singleton;
import filecopier.options.Options;
import filecopier.ActionCommand;
import filecopier.gui.copy.CopyGuiWorker;
import filecopier.script.Script;
import filecopier.script.CopyScript;
import filecopier.gui.FileCopier;

/**
 *
 * @author david
 */
public class CopyPanel extends JPanel implements ActionCommand, PropertyChangeListener {

    private ScriptSelectorBox[] scriptSelectors = null;

    /** Creates a new instance of CopyPanel */
    public CopyPanel() {
        super();
        initialize();
    }

    private void initialize() {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        scriptSelectors = new ScriptSelectorBox[Options.TO_IDX + 1];

        this.add(getScriptSelector(Options.FROM_IDX, Options.CopyScriptType.From, "Browse for from script",
                java.awt.event.KeyEvent.VK_B, "Edit from Script", java.awt.event.KeyEvent.VK_T,
                "Create new from script", java.awt.event.KeyEvent.VK_N));
        this.add(getScriptSelector(Options.TO_IDX, Options.CopyScriptType.To, "Browse for to script",
                java.awt.event.KeyEvent.VK_R, "Edit to script", java.awt.event.KeyEvent.VK_I,
                "Create new to script", java.awt.event.KeyEvent.VK_W));
    }

    private ScriptSelectorBox getScriptSelector(int idx, final Options.CopyScriptType copyScriptType, final String browseToolTip,
            int browseMnemonic, final String editToolTip, int editMnemonic, final String newToolTip, int newMnemonic) {
        if (scriptSelectors[idx] == null) {
            scriptSelectors[idx] = new ScriptSelectorBox(idx, copyScriptType, Singleton.getInstance().getOptions().getCopyPath(),
                    browseToolTip, browseMnemonic, editToolTip, editMnemonic, newToolTip, newMnemonic);
        }
        return scriptSelectors[idx];
    }

    public ScriptSelectorBox getScriptSelector(int idx) {
        return scriptSelectors.length > idx ? scriptSelectors[idx] : null;
    }

    public void doAction() {
        try {
            Script fromScript = new CopyScript(new File(scriptSelectors[Options.FROM_IDX].getScriptName()));
            Script toScript = new CopyScript(new File(scriptSelectors[Options.TO_IDX].getScriptName()));
            ActionCommand copyWorker = new CopyGuiWorker(fromScript, toScript);
            RestorePanel restorePanel = (RestorePanel) ((FileCopier) Singleton.getInstance().getFrame()).getMainTabbedPane().getRestorePanel();
            // Need to add the RestorePanel object as a PropertyChangeListener to copyWorker.
            ((CopyGuiWorker) copyWorker).getPropertyChangeSupport().addPropertyChangeListener("restoreScriptCreated", restorePanel);
            ((CopyGuiWorker) copyWorker).getPropertyChangeSupport().addPropertyChangeListener("tempScriptDeleted", this);
            copyWorker.doAction();
        } catch (custom.xml.DOMHelperException e) {
            JOptionPane.showMessageDialog(Singleton.getInstance().getFrame(), e.getMessage(), "Invalid Script", JOptionPane.ERROR_MESSAGE);
        } catch (java.io.IOException e) {
            JOptionPane.showMessageDialog(Singleton.getInstance().getFrame(), e.getMessage(), "Invalid Script", JOptionPane.ERROR_MESSAGE);
        } catch (NullPointerException e) {
            JOptionPane.showMessageDialog(Singleton.getInstance().getFrame(), "From/To script boxes must contain a valid script",
                    "From/To Script box empty", JOptionPane.ERROR_MESSAGE);
        } catch (filecopier.ActionCancelException e) {
            JOptionPane.showMessageDialog(Singleton.getInstance().getFrame(), e.getMessage(), "File Copy cancelled",
                    JOptionPane.WARNING_MESSAGE);
            Singleton.getInstance().getFileLogger().warn("User cancelled file copy operation"); 
        }
    }

    public void propertyChange(PropertyChangeEvent e) {
        String propertyName = e.getPropertyName();
        if (propertyName.equals("tempScriptDeleted")) {
            JComboBox scriptComboBox = null;
            String tempScriptName = (String) e.getNewValue();
            String scriptName = scriptSelectors[Options.FROM_IDX].getScriptName();
            if (scriptName != null && scriptName.equals(tempScriptName)) {
                scriptComboBox = scriptSelectors[Options.FROM_IDX].getScriptComboBox();
            } else {
                scriptName = scriptSelectors[Options.TO_IDX].getScriptName();
                if (scriptName != null && scriptName.equals(tempScriptName)) {
                    scriptComboBox = scriptSelectors[Options.TO_IDX].getScriptComboBox();
                }
            }

            if (scriptComboBox != null) {
                scriptComboBox.removeItem(tempScriptName);
            }
        }
    }
}
